package com.csun.cmny.provider.controller.admin;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.DmcDevice;
import com.csun.cmny.provider.model.domain.DmcPatrolGreatLocation;
import com.csun.cmny.provider.model.domain.DmcPatrolPointBind;
import com.csun.cmny.provider.model.domain.DmcPatrolPointLocation;
import com.csun.cmny.provider.model.dto.device.DmcEditDeviceDto;
import com.csun.cmny.provider.model.dto.patrol.*;
import com.csun.cmny.provider.model.dto.propertyManagement.DeviceDeriveExcelDto;
import com.csun.cmny.provider.model.enums.device.DmcDeviceTypeEnum;
import com.csun.cmny.provider.model.vo.propertyManagement.PropertyManagementDeviceVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.*;
import com.csun.cmny.provider.validator.DmcDeviceEditValidator;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "/patrolPoint", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class DmcPatrolPointController extends BaseController {

    @Resource
    private DmcPatrolPointLocationService dmcPatrolPointLocationServiceImpl;
    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private DmcDeviceService dmcDeviceService;
    @Resource
    private DmcPatrolPointBindService dmcPatrolPointBindServiceImp;
    @Resource/**/
    private OmcRpcService omcRpcService;
    @Resource
    private DmcPatrolRecordService dmcPatrolRecordServiceImp;
    @Resource
    private DmcPatrolReminderService dmcPatrolReminderServiceImp;
    /*
     * 巡更点的分页查询
     * */
    @RequestMapping(value = "/queryGroupPatrolPointListWithPage", method = RequestMethod.POST)
    public Wrapper queryGroupPatrolPointListWithPage(@RequestBody PatrolPointPageQuery patrolPointPageQuery){
        patrolPointPageQuery.setGroupId(getLoginAuthDto().getGroupId());
        patrolPointPageQuery.setDeviceType(DmcDeviceTypeEnum.PATROL_POINT.getIndex());
        PageInfo pageInfo = dmcPatrolPointLocationServiceImpl.queryDeviceListWithPage(patrolPointPageQuery);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),pageInfo);
    }
    /*
     * 巡更点的修改
     * */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Wrapper updatePatrol(@Validated(value = {DmcDeviceEditValidator.class})
                                @RequestBody DmcEditDeviceDto dmcEditDeviceDto, BindingResult br) {
        DmcDevice dmcDevice = new DmcDevice();
        BeanUtils.copyProperties(dmcEditDeviceDto, dmcDevice);
        dmcDevice.setDeviceType(DmcDeviceTypeEnum.PATROL_POINT.getIndex());
        Integer result = dmcDeviceService.editDevice(dmcDevice, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), result);
    }
    /*
     * 绑定巡更点
     * */
    @RequestMapping(value = "/bind", method = RequestMethod.POST)
    public Wrapper patrolBind(@RequestBody PatrolPointQueryDto patrolPointQueryDto){
        LoginAuthDto loginAuthDto = getLoginAuthDto();
        patrolPointQueryDto.setGroupId(loginAuthDto.getGroupId());

        DmcPatrolPointLocation dmcPatrolPointLocation = new DmcPatrolPointLocation();
        BeanUtils.copyProperties(patrolPointQueryDto, dmcPatrolPointLocation);
        dmcPatrolPointLocation.setCreator(loginAuthDto.getUserName());
        dmcPatrolPointLocation.setCreatorId(loginAuthDto.getUserId());
        //查询点位是否绑定
        if(patrolPointQueryDto.getBedId()!=null || patrolPointQueryDto.getRoomId()!=null){
            DmcPatrolPointLocation dpl = dmcPatrolPointLocationServiceImpl.selectByBedId(patrolPointQueryDto.getBedId(),patrolPointQueryDto.getRoomId());
            if(dpl!=null){
                return WrapMapper.wrap(Wrapper.ERROR_CODE, "该位置已被绑定");
            }
        }
        //查询设备是否绑定
        DmcPatrolPointLocation patrolPoint = dmcPatrolPointLocationServiceImpl.selectByKey(dmcPatrolPointLocation.getDeviceId());
        if(patrolPoint!=null){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        Integer result = dmcPatrolPointLocationServiceImpl.patrolPointBind(dmcPatrolPointLocation);
        if (result<1){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }
    /*
     * 解绑巡更点
     * */
    @RequestMapping(value = "/untied", method = RequestMethod.POST)
    public Wrapper patrolUntied(@RequestBody PatrolPointQueryDto patrolPointQueryDto){

        DmcPatrolPointLocation dmcPatrolPointLocation = new DmcPatrolPointLocation();
        BeanUtils.copyProperties(patrolPointQueryDto, dmcPatrolPointLocation);
        //解绑巡更棒
        Integer result = dmcPatrolPointLocationServiceImpl.delete(dmcPatrolPointLocation);
        //删除巡更记录
        dmcPatrolRecordServiceImp.deleteByPointId(dmcPatrolPointLocation.getDeviceId());
        //删除巡更提醒
        dmcPatrolReminderServiceImp.deleteByPointId(dmcPatrolPointLocation.getDeviceId());
        if(result<1){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }
    /*
    * 巡更点绑定巡更棒
    * */
    @RequestMapping(value = "/bindPatrol", method = RequestMethod.POST)
    public Wrapper pointBindPatrol(@RequestBody PointPatrolDto pointPatrolDto){
        //获取巡更点编号
        DeviceDeriveExcelDto deriveExcelDto = new DeviceDeriveExcelDto();
        deriveExcelDto.setGroupId(getLoginAuthDto().getGroupId());
        deriveExcelDto.setDeviceIds(pointPatrolDto.getGreatIds());

        List<DmcPatrolPointBind>  dmcPatrolPointBinds = new ArrayList<>();

        if(pointPatrolDto.getGreatIds()!=null){
            List<PropertyManagementDeviceVo> devices = dmcDeviceService.selectDeviceByDeviceIds(deriveExcelDto);
            for (PropertyManagementDeviceVo device:devices) {
                DmcPatrolPointBind dmcPatrolPointBind = new DmcPatrolPointBind();
                BeanUtils.copyProperties(pointPatrolDto,dmcPatrolPointBind);
                dmcPatrolPointBind.setGreatId(device.getDeviceId());
                dmcPatrolPointBind.setGreatNo(device.getDeviceNo());
                dmcPatrolPointBinds.add(dmcPatrolPointBind);
            }
        }
        Integer i = dmcPatrolPointBindServiceImp.deleteById(pointPatrolDto.getPointId());
       if(dmcPatrolPointBinds.size()!=0){
           Integer result = dmcPatrolPointBindServiceImp.pointBindPatrol(dmcPatrolPointBinds);
           if(result<1){
               return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                       .getMessage("operation.failed", null));
           }
       }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),i);

    }
    /*
     * 巡更点解绑巡更棒
     * */
    @RequestMapping(value = "/untiedPatrol", method = RequestMethod.POST)
    public Wrapper pointUntiedPatrol(@RequestBody PointPatrolDto pointPatrolDto){
        //获取巡更点编号
        DeviceDeriveExcelDto deriveExcelDto = new DeviceDeriveExcelDto();
        deriveExcelDto.setGroupId(getLoginAuthDto().getGroupId());
        deriveExcelDto.setDeviceIds(pointPatrolDto.getGreatIds());

        List<PropertyManagementDeviceVo> devices = dmcDeviceService.selectDeviceByDeviceIds(deriveExcelDto);
        List<DmcPatrolPointBind>  dmcPatrolPointBinds = new ArrayList<>();

        for (PropertyManagementDeviceVo device:devices) {
            DmcPatrolPointBind dmcPatrolPointBind = new DmcPatrolPointBind();
            BeanUtils.copyProperties(pointPatrolDto,dmcPatrolPointBind);
            dmcPatrolPointBind.setGreatId(device.getDeviceId());
            dmcPatrolPointBind.setGreatNo(device.getDeviceNo());
            dmcPatrolPointBinds.add(dmcPatrolPointBind);
        }
        Integer result = dmcPatrolPointBindServiceImp.pointUntiedPatrol(dmcPatrolPointBinds);
        if(result<1){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

}
